/*
	NounTrail.cpp
	(c)2000 Palestar Inc, Richard Lyle
*/


#include "Debug/Assert.h"
#include "NounTrail.h"
#include "Constants.h"
#include "GameContext.h"

//----------------------------------------------------------------------------

IMPLEMENT_FACTORY( NounTrail, NounGame );
REGISTER_FACTORY_KEY( NounTrail, 4160086826159486992 );

BEGIN_PROPERTY_LIST( NounTrail, NounGame )
	CLEAR_PROPERTY_FLAGS( m_Flags, PF_UPDATE );
	CLEAR_PROPERTY_FLAGS( m_Position, PF_UPDATE );
	CLEAR_PROPERTY_FLAGS( m_vOrientation, PF_UPDATE );
END_PROPERTY_LIST();

NounTrail::NounTrail()
{}

//----------------------------------------------------------------------------

bool NounTrail::serializeKey() const
{
	return false;
}

bool NounTrail::read( InStream & input ) 
{
	if (! Noun::read( input ) )
		return false;

	resetTrail();

	return true;
}

void NounTrail::resetTrail()
{
	m_Trail.setTrailMaterial( WidgetCast<Material>( 
		nounContext()->resource( "TRAIL" ) ) );
}

//----------------------------------------------------------------------------

void NounTrail::render( RenderContext &context, 
	const Matrix33 & frame, 
	const Vector3 & position )
{
	// call our base class
	Noun::render( context, frame, position );

	// make sure our world position is current
	invalidateWorld();
	// render the trail
	m_Trail.renderTrail( context, frame, position, worldPosition() );
}

//----------------------------------------------------------------------------

NounType NounTrail::nounType() const
{
	return TYPE_EFFECT;
}

void NounTrail::simulate( dword nTick )
{
	if ( context()->client() )
		m_Trail.updateTrail( worldPosition() );

	Noun::simulate( nTick );
}

void NounTrail::onWorldTransform( const Vector3 & transform )
{
	Noun::onWorldTransform( transform );
	// update the trail so it appears in the same location
	m_Trail.offsetTrail( transform );
}

//----------------------------------------------------------------------------
//EOF
